<template>
  <div class="mod-demo__userequipment" style="overflow-y: scroll;height: 800px;">
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
      <el-form-item label="用户昵称">
        <el-input v-model="state.dataForm.username" placeholder="请输入用户昵称" clearable></el-input>
      </el-form-item>
      <el-form-item label="用户账号">
        <el-input v-model="state.dataForm.mobile" placeholder="请输入用户账号" clearable></el-input>
      </el-form-item>
      <el-form-item label="设备号">
        <el-input v-model="state.dataForm.bumber" placeholder="请输入设备号" clearable></el-input>
      </el-form-item>
      <el-form-item label="授权状态">
        <el-select v-model="state.dataForm.state" clearable placeholder="请选择授权状态">
          <el-option label="待授权" value="0"></el-option>
          <el-option label="授权通过" value="1"></el-option>
          <el-option label="授权驳回" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="query">查询</el-button>
        <el-button @click="examinehandle(1)" type="success">批量通过</el-button>
        <el-button @click="examinehandle(2)" type="danger">批量驳回</el-button>
      </el-form-item>
    </el-form>
    <div style="padding-left: 20px; padding-bottom: 6px;font-size: 12px">
      当前有&nbsp;<span style="color: orangered;font-size: 15px"> {{proCountDate.total}}</span>&nbsp;个设备代授权
    </div>
    <el-table v-loading="state.dataListLoading" :data="state.dataList" border @selection-change="state.dataListSelectionChangeHandle" style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="username" label="用户名呢称" header-align="center" align="center"></el-table-column>
      <el-table-column prop="mobile" label="用户账号" header-align="center" align="center"></el-table-column>
      <el-table-column prop="bumber" label="设备号" header-align="center" align="center"></el-table-column>
      <el-table-column prop="createDate" label="创建时间" header-align="center" align="center"></el-table-column>
      <el-table-column prop="updateDate" label="处理时间" header-align="center" align="center"></el-table-column>
      <el-table-column prop="state" label="状态" sortable="custom" header-align="center" align="center">
        <template v-slot="scope">
          <el-tag v-if="scope.row.state === 0" size="small" type="danger">待授权</el-tag>
          <el-tag v-if="scope.row.state === 1" size="small" type="success">授权通过</el-tag>
          <el-tag v-if="scope.row.state === 2" size="small" type="info">授权驳回</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" fixed="right" header-align="center" align="center" width="150">
        <template v-slot="scope">
          <el-button  type="primary" link @click="pauseHandle(scope.row.id,'1')">授权通过</el-button>
          <el-button  type="primary" link @click="pauseHandle(scope.row.id,'2')">授权驳回</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="state.page" :page-sizes="[10, 20, 50, 100]" :page-size="state.limit"
                   :total="state.total" layout="total, sizes, prev, pager, next, jumper"
                   @size-change="state.pageSizeChangeHandle"
                   @current-change="state.pageCurrentChangeHandle"></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update ref="addOrUpdateRef" @refreshDataList="state.getDataList">确定</add-or-update>
  </div>
</template>

<script lang="ts" setup>
import useView from "@/hooks/useView";
import {reactive, ref, toRefs} from "vue";
import AddOrUpdate from "./userequipment-add-or-update.vue";
import {ElMessage, ElMessageBox} from "element-plus";
import baseService from "@/service/baseService";
import {IObject} from "@/types/interface";

const view = reactive({
  deleteIsBatch: true,
  getDataListURL: "/sysapp/userequipment/page",
  getDataListIsPage: true,
  exportURL: "/sysapp/userequipment/export",
  deleteURL: "/sysapp/userequipment"
});

const state = reactive({...useView(view), ...toRefs(view)});

const proCountDate = reactive({})
baseService.get("/sysapp/userequipment/page",{
  order: state.order,
  orderField: state.orderField,
  page: state.getDataListIsPage ? state.page : null,
  limit: state.getDataListIsPage ? state.limit : null,
  state:0
}).then((res) => {
  Object.assign(proCountDate,res.data)
});
const examinehandle = (type:number) => {
  debugger
  if ( state.dataListSelections && state.dataListSelections.length <= 0) {
    return ElMessage({
      message: "请选[授权]择操作项",
      type: "warning",
      duration: 500
    });
  }
  let idArray =[];
  state.dataListSelections?.forEach((item:{})=>{
    idArray.push(item.id)
  })
  let idStr ="";
  idStr = idArray.join(",");
  ElMessageBox.confirm("确定进行[授权]操作?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(() => {
      baseService.put("sysapp/userequipment/update",
        {"ids":idStr,state:type},
      ).then((res) => {
        ElMessage.success({
          message: "成功",
          duration: 500,
          onClose: () => {
            state.getDataList();
          }
        });
      });
    })
    .catch(() => {
      //
    });
}

const pauseHandle = (id?: string,status?:number) => {
  if (!id && !status && state.dataListSelections && state.dataListSelections.length <= 0) {
    return ElMessage({
      message: "请选择操作项",
      type: "warning",
      duration: 500
    });
  }
  ElMessageBox.confirm("确定进行[授权]操作?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(() => {
      baseService.put("sysapp/userequipment/update",
        {"id":id,state:status},
      ).then((res) => {
        ElMessage.success({
          message: "成功",
          duration: 500,
          onClose: () => {
            state.getDataList();
          }
        });
      });
    })
    .catch(() => {
      //
    });
};
const query = () => {
  state.getDataList()
  baseService.get("/sysapp/userequipment/page",{
    order: state.order,
    orderField: state.orderField,
    page: state.getDataListIsPage ? state.page : null,
    limit: state.getDataListIsPage ? state.limit : null,
    state:0
  }).then((res) => {
    Object.assign(proCountDate,res.data)
  });
}
</script>
